package com.java.shop.order.listener;

import com.alipay.easysdk.factory.Factory;
import com.java.common.constant.MqConstant;
import com.java.shop.order.entity.OrderEntity;
import com.java.shop.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = {MqConstant.order_ordQueue})
public class OrderCloseListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(Message message, Channel channel, OrderEntity orderEntity) throws IOException {
        System.out.println("准备关单->"+orderEntity.getId());
        try {
            //主动关单（mq发送消息给库存服务，关闭那个订单）
            orderService.orderClose(orderEntity);
            //取消支付宝支付功能
            Factory.Payment.Common().close(orderEntity.getOrderSn());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
    }
}
